package com.itheima.spec.controller;

import com.itheima.goods.pojo.Spec;
import com.itheima.spec.service.SpecService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/29 17:17
 */
@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecService specService;

    @GetMapping
    public Result findAll() {
        List<Spec> specs = specService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",specs) ;
    }

    @PostMapping
    public Result add(@RequestBody Spec spec) {
        boolean add = specService.add(spec);
        return new Result(add,StatusCode.OK,"添加成功",null);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPage4Post(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestBody Spec spec
            ){
        PageResult pageResult = specService.findPage4Post(page,size,spec);
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage4Get(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestBody Spec spec
    ) {
        PageResult pageResult = specService.findPage4Get(page,size);
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Spec spec = specService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",spec);
    }

    @PutMapping("/{id}")
    public Result update(
            @PathVariable("id") Integer id,
            @RequestBody Spec spec) {
        boolean update = specService.updateSpec(id,spec);
        return new Result(update,StatusCode.OK,"修改成功",null);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(
            @PathVariable("id") Integer id
    ) {
        boolean delete = specService.deleteById(id);
        return new Result(delete,StatusCode.OK,"删除成功",null);
    }
}
